
import { createApp } from 'vue'
import { createPinia } from 'pinia'

import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

const app = createApp(App)

app.use(createPinia())
app.use(router)

app.mount('#container')
app.use(ElementPlus)

// 注册自定义指令
app.directive('draggable', {
  mounted(el, binding) {
    let startX, startY, initialX, initialY;

    el.style.position = 'absolute';
    el.style.cursor = 'move';

    el.addEventListener('mousedown', (e) => {
      e.preventDefault();
      startX = e.clientX;
      startY = e.clientY;
      initialX = el.offsetLeft;
      initialY = el.offsetTop;

      document.addEventListener('mousemove', move);
      document.addEventListener('mouseup', up);
    });

    function move(e) {
      const dx = e.clientX - startX;
      const dy = e.clientY - startY;
      el.style.left = `${initialX + dx}px`;
      el.style.top = `${initialY + dy}px`;
    }

    function up(e) {
      document.removeEventListener('mousemove', move);
      document.removeEventListener('mouseup', up);

      if (binding.value && typeof binding.value === 'function') {
        binding.value({
          x: el.offsetLeft,
          y: el.offsetTop
        });
      }
    }
  }
});
